package domainservice

import (
	"context"

	"go-mall/common/errcode"
	"go-mall/common/util"
	"go-mall/dal/dao"
	"go-mall/logic/do"
)

type DemoDomainSvc struct {
	ctx     context.Context
	DemoDao *dao.DemoDao
}

func NewDemoDomainSvc(ctx context.Context) *DemoDomainSvc {
	return &DemoDomainSvc{
		ctx:     ctx,
		DemoDao: dao.NewDemoDao(ctx),
	}
}

func (dds *DemoDomainSvc) GetDemos() ([]*do.DemoOrder, error) {
	demos, err := dds.DemoDao.GetAllDemos()

	// fmt.Printf("GetDemos %+v\n", demos)
	if err != nil {
		err = errcode.Wrap("query demo err", err)
		return nil, err
	}

	demoOrders := make([]*do.DemoOrder, 0, len(demos))

	for _, demo := range demos {
		// demoOrders = append(demoOrders, &do.DemoOrder{
		// 	Id:      demoOrder.Id,
		// 	UserId:  demoOrder.UserId,
		// 	OrderNo: demoOrder.OrderNo,
		// 	// OrderGoodsId: demoOrder.OrderGoodsId,
		// 	BillMoney: demoOrder.BillMoney,
		// 	State:     demoOrder.State,
		// 	PaidAt:    demoOrder.PaidAt,
		// })
		demoOrder := new(do.DemoOrder)
		util.CopyProperties(demoOrder, demo)
		demoOrders = append(demoOrders, demoOrder)
	}
	return demoOrders, nil
}

func (dds *DemoDomainSvc) CreateDemoOrder(
	demoOrer *do.DemoOrder) (*do.DemoOrder, error) {

	demoOrer.OrderNo = "20240627596615375920904456"
	demoOrderModel, err := dds.DemoDao.CreateDemoOrder(demoOrer)
	if err != nil {
		err = errcode.Wrap("创建DemoOrder失败", err)
		return nil, err
	}
	// TODO1
	err = util.CopyProperties(demoOrer, demoOrderModel)
	return demoOrer, err
}
